package com.hzqy.service.multirate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.JsonUtils;
import com.hzqy.persistence.dao.multirate.MultirateDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmCatalogContentVo;
import com.hzqy.web.vo.CmCatalogVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.CmMultirateContentVo;
import com.hzqy.web.vo.CmMultirateVo;
import com.hzqy.web.vo.CmPicturesVo;
import com.hzqy.web.vo.PmsZTreeVo;

@Service
public class MultirateServiceBean extends GenericServiceBean<CmMultirateVo, MultirateDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private MultirateDao multirateDao;

	@Override
	public MultirateDao getEntityDao() {
		return multirateDao;
	}
	
	public CmMultirateVo selectMultirate(int f_multirate_id) {
		return multirateDao.selectMultirate(f_multirate_id);
	}
	
	public Map<String,Object> selectMultirateList(CmMultirateVo cmMultirateVo, int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = multirateDao.findMultirateCount(cmMultirateVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_multirate_status", cmMultirateVo.getF_multirate_status());
			map.put("f_multirate_name", cmMultirateVo.getF_multirate_name());
			map.put("f_multirate_type", cmMultirateVo.getF_multirate_type());
			map.put("f_multirate_companyid", cmMultirateVo.getF_multirate_companyid());
			map.put("cmMultirateVo", cmMultirateVo);//
			List<CmMultirateVo> results = multirateDao.findMultirateList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmMultirateVo>());
		}
		return map;
	}
	
	public int deleteMultirate(int f_multirate_id) {
		CmMultirateContentVo cmMultirateContentVo = new CmMultirateContentVo();
		cmMultirateContentVo.setF_ccm_multirateid(f_multirate_id);
		multirateDao.deletePack(cmMultirateContentVo);
		multirateDao.deleteMultirate(f_multirate_id);
		return 1;
	}
	
	public int insertMultirate(CmMultirateVo cmMultirateVo,String ids) {
		int flag = 0;
		if(cmMultirateVo==null || cmMultirateVo.getF_multirate_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			Date date = new Date();
			SimpleDateFormat smp = new SimpleDateFormat("yyyyMMddHHmmss");
			String number = smp.format(date);
			cmMultirateVo.setF_multirate_number(number);
			flag = multirateDao.insertMultirate(cmMultirateVo);
		}
		if(ids != null && !ids.equals("")) {
			if(flag == 1){
				CmMultirateVo cmv = multirateDao.selectMultirateByNumber(cmMultirateVo);
				batchPack(cmv.getF_multirate_id(),ids);
			}
		}
		return flag;
	}
	
	public int updateMultirate(CmMultirateVo cmMultirateVo) {
		int flag = 0;
		if(cmMultirateVo==null || cmMultirateVo.getF_multirate_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			flag = multirateDao.updateMultirate(cmMultirateVo);
		}
		return flag;
	}
	
	public Map<String,Object> selectPackContent(CmContentVo cmContentVo,int f_ccm_multirateid,int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_ccm_multirateid", f_ccm_multirateid);
		map.put("f_cc_name", cmContentVo.getF_cc_name());
		map.put("f_cc_status",cmContentVo.getF_cc_status());
		map.put("f_cc_companyid", cmContentVo.getF_cc_companyid());
		map.put("f_cc_type", cmContentVo.getF_cc_type());
		Integer rowCount = multirateDao.findPackCount(map);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			
			List<CmContentVo> results = multirateDao.selectPackContent(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmContentVo>());
		}
		return map;
	}
	
	public Map<String,Object> selectUnPackContent(CmContentVo cmContentVo,int f_ccm_multirateid,int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_ccm_multirateid", f_ccm_multirateid);
		map.put("f_cc_name", cmContentVo.getF_cc_name());
		map.put("f_cc_status",cmContentVo.getF_cc_status());
		map.put("f_cc_companyid", cmContentVo.getF_cc_companyid());
		map.put("f_cc_type", cmContentVo.getF_cc_type());
		Integer rowCount = multirateDao.findUnPackCount(map);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			
			List<CmContentVo> results = multirateDao.selectUnPackContent(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmContentVo>());
		}
		return map;
	}
	
	public int deletePack(int f_ccm_contentid,int f_ccm_multirateid) {
		int flag = 0;
		if(f_ccm_contentid <0 && f_ccm_multirateid <0) {
			flag = -1;
		}
		if(flag == 0) {
			CmMultirateContentVo cmMultirateContentVo = new CmMultirateContentVo();
			cmMultirateContentVo.setF_ccm_contentid(f_ccm_contentid);
			cmMultirateContentVo.setF_ccm_multirateid(f_ccm_multirateid);
			flag = multirateDao.deletePack(cmMultirateContentVo);
		}
		return flag;
	}
	
	
	public int insertPack(int f_ccm_contentid,int f_ccm_multirateid) {
		int flag = 0;
		if(f_ccm_contentid <0 && f_ccm_multirateid <0) {
			flag = -1;
		}
		if(flag == 0) {
			CmMultirateContentVo cmMultirateContentVo = new CmMultirateContentVo();
			cmMultirateContentVo.setF_ccm_contentid(f_ccm_contentid);
			cmMultirateContentVo.setF_ccm_multirateid(f_ccm_multirateid);
			flag = multirateDao.insertPack(cmMultirateContentVo);
		}
		return flag;
	}
	
	public int batchPack(int f_ccm_multirateid, String ids) {
		int flag = 0;
		if(f_ccm_multirateid <= 0 || ids==null || ids.equals("")) {
			flag = -1;
		}
		if(flag == 0) {
			String[] idArr = ids.split(",");
			for(int i=0; i<idArr.length;i++) {
				int f_ccm_contentid = Integer.valueOf(idArr[i]);
				CmMultirateContentVo cmMultirateContentVo = new CmMultirateContentVo();
				cmMultirateContentVo.setF_ccm_contentid(f_ccm_contentid);
				cmMultirateContentVo.setF_ccm_multirateid(f_ccm_multirateid);
				multirateDao.insertPack(cmMultirateContentVo);
			}
			flag = 1;
		}
		return flag;
	}
	
	public int batchUnPack(int f_ccm_multirateid, String ids) {
		int flag = 0;
		if(f_ccm_multirateid <= 0 || ids==null || ids.equals("")) {
			flag = -1;
		}
		if(flag == 0) {
			String[] idArr = ids.split(",");
			for(int i=0; i<idArr.length;i++) {
				int f_ccm_contentid = Integer.valueOf(idArr[i]);
				CmMultirateContentVo cmMultirateContentVo = new CmMultirateContentVo();
				cmMultirateContentVo.setF_ccm_contentid(f_ccm_contentid);
				cmMultirateContentVo.setF_ccm_multirateid(f_ccm_multirateid);
				multirateDao.deletePack(cmMultirateContentVo);
			}
			flag = 1;
		}
		return flag;
	}
	
	public Map<String,Object> selectBindMultirate(CmMultirateVo cmMultirateVo,int f_ccc_catalogid,int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_ccc_catalogid", f_ccc_catalogid);
		map.put("f_multirate_name", cmMultirateVo.getF_multirate_name());
		map.put("f_multirate_score",cmMultirateVo.getF_multirate_score());
		map.put("f_multirate_companyid", cmMultirateVo.getF_multirate_companyid());
		Integer rowCount = multirateDao.findBindCount(map);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			
			List<CmMultirateVo> results = multirateDao.selectBindMultirate(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmMultirateVo>());
		}
		return map;
	}
	
	public Map<String,Object> selectUnBindMultirate(CmMultirateVo cmMultirateVo,int f_ccc_catalogid,int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_ccc_catalogid", f_ccc_catalogid);
		map.put("f_multirate_name", cmMultirateVo.getF_multirate_name());
		map.put("f_multirate_score",cmMultirateVo.getF_multirate_score());
		map.put("f_multirate_companyid", cmMultirateVo.getF_multirate_companyid());
		Integer rowCount = multirateDao.findUnBindCount(map);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			
			List<CmMultirateVo> results = multirateDao.selectUnBindMultirate(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmMultirateVo>());
		}
		return map;
	}
	
	/**
	 * 解除绑定
	 * @param f_ccc_catalogid
	 * @param f_ccc_contentid
	 * @return
	 */
	public int deleteBind(int f_ccc_catalogid,int f_ccc_contentid) {
		int flag = 0;
		if(f_ccc_catalogid <0 && f_ccc_contentid <0) {
			flag = -1;
		}
		if(flag == 0) {
			CmCatalogContentVo cmCatalogContentVo = new CmCatalogContentVo();
			cmCatalogContentVo.setF_ccc_catalogid(f_ccc_catalogid);
			cmCatalogContentVo.setF_ccc_contentid(f_ccc_contentid);
			flag = multirateDao.deleteBind(cmCatalogContentVo);
		}
		return flag;
	}
	
	
	public int insertBind(int f_ccc_catalogid,int f_ccc_contentid) {
		int flag = 0;
		if(f_ccc_catalogid <0 && f_ccc_contentid <0) {
			flag = -1;
		}
		if(flag == 0) {
			CmCatalogContentVo cmCatalogContentVo = new CmCatalogContentVo();
			cmCatalogContentVo.setF_ccc_catalogid(f_ccc_catalogid);
			cmCatalogContentVo.setF_ccc_contentid(f_ccc_contentid);
			flag = multirateDao.insertBind(cmCatalogContentVo);
		}
		return flag;
	}
	
	/**
	 * 批量绑定
	 * @param f_ccc_catalogid
	 * @param ids
	 * @return
	 */
	public int batchBind(int f_ccc_catalogid, String ids) {
		int flag = 0;
		if(f_ccc_catalogid <= 0 || ids==null || ids.equals("")) {
			flag = -1;
		}
		if(flag == 0) {
			String[] idArr = ids.split(",");
			for(int i=0; i<idArr.length;i++) {
				int f_ccc_contentid = Integer.valueOf(idArr[i]);
				CmCatalogContentVo cmCatalogContentVo = new CmCatalogContentVo();
				cmCatalogContentVo.setF_ccc_catalogid(f_ccc_catalogid);
				cmCatalogContentVo.setF_ccc_contentid(f_ccc_contentid);
				multirateDao.insertBind(cmCatalogContentVo);
			}
			flag = 1;
		}
		return flag;
	}
	
	public int batchUnBind(int f_ccc_catalogid, String ids) {
		int flag = 0;
		if(f_ccc_catalogid <= 0 || ids==null || ids.equals("")) {
			flag = -1;
		}
		if(flag == 0) {
			String[] idArr = ids.split(",");
			for(int i=0; i<idArr.length;i++) {
				int f_ccc_contentid = Integer.valueOf(idArr[i]);
				CmCatalogContentVo cmCatalogContentVo = new CmCatalogContentVo();
				cmCatalogContentVo.setF_ccc_catalogid(f_ccc_catalogid);
				cmCatalogContentVo.setF_ccc_contentid(f_ccc_contentid);
				multirateDao.deleteBind(cmCatalogContentVo);
			}
			flag = 1;
		}
		return flag;
	}
	
	/**
	 * 查询内容绑定的分类列表
	 * @param f_cc_id
	 * @return
	 */
	public List<CmCatalogVo> selectContentBindCatalog(int f_multirate_id) {
		return multirateDao.selectContentBindCatalog(f_multirate_id);
	}
	
	/**
	 * 获取分类目录并转化为ZtreeVo
	 * @return
	 */
	public List<PmsZTreeVo> selectCatalogTree() {
		List<CmCatalogVo> cataList = multirateDao.selectCatalogList();
		List<PmsZTreeVo> treeList = new ArrayList<PmsZTreeVo>();
		if(cataList!=null && cataList.size()!=0) {
			for(int i=0;i<cataList.size();i++) {
				CmCatalogVo catalog = cataList.get(i);
				PmsZTreeVo tree = new PmsZTreeVo();
				tree.setId(catalog.getF_catalog_id()+"");
				tree.setpId(catalog.getF_catalog_parentid()+"");
				tree.setName(catalog.getF_catalog_name());
				treeList.add(tree);
			}
		}
		return treeList;
	}
	
	/**
	 * 根据f_ccc_contentid 查询 绑定的目录
	 * @param f_ccc_contentid
	 * @return
	 */
	public List<CmCatalogContentVo> selectCatalogBind(int f_ccc_contentid) {
		return multirateDao.selectCatalogBind(f_ccc_contentid);
	}
	
	/**
	 * 修改内容绑定的分类目录
	 * @param f_ccc_contentid
	 * @param catalogStr
	 * @return
	 */
	public int updateCatalogBind(int f_ccc_contentid,String cataloginStr,String catalogdeStr) {
		int flag = 0;
		if(f_ccc_contentid <= 0) {
			flag = -1;
		} else if ((cataloginStr == null || cataloginStr.equals("")) && (catalogdeStr == null || catalogdeStr.equals(""))) {
			flag = -2;
		}
		
		if(flag == 0) {
			flag = 1;
			if (catalogdeStr != null && !catalogdeStr.equals("")) {
				String[] decatalogIds = catalogdeStr.split(",");
				for(String str:decatalogIds) {
					CmCatalogContentVo catalogContentVo = new CmCatalogContentVo();
					catalogContentVo.setF_ccc_contentid(f_ccc_contentid);
					catalogContentVo.setF_ccc_catalogid(Integer.valueOf(str));
					multirateDao.deleteBind(catalogContentVo);//逐个解绑
				}
			}
			if (cataloginStr != null && !cataloginStr.equals("")) {
				String[] incatalogIds = cataloginStr.split(","); 
				for(String str:incatalogIds) {
					CmCatalogContentVo catalogContentVo = new CmCatalogContentVo();
					catalogContentVo.setF_ccc_contentid(f_ccc_contentid);
					catalogContentVo.setF_ccc_catalogid(Integer.valueOf(str));
					multirateDao.insertBind(catalogContentVo);//逐个绑定
				}
			}
		}
		return flag;
	}
	
	public CmCatalogContentVo selectViewIndex(CmCatalogContentVo cmCatalogContentVo) {
		return multirateDao.selectViewIndex(cmCatalogContentVo);
	}
	
	public int updateViewIndex(CmCatalogContentVo cmCatalogContentVo) {
		int flag = 0;
		if(cmCatalogContentVo==null) {
			flag = -1;
		}
		if(flag == 0) {
			multirateDao.updateViewIndex(cmCatalogContentVo);
			return 1;
		}
		return flag;
	}

	public int batchUpdateStatus(int status, String ids) {
		int flag = 0;
		if(status <= 0 || ids==null || ids.equals("")) {
			flag = -1;
		}
		if(flag == 0) {
			String[] idArr = ids.split(",");
			for(int i=0; i<idArr.length;i++) {
				int f_multirate_id = Integer.valueOf(idArr[i]);
				CmMultirateVo cmMultirateVo = new CmMultirateVo();
				cmMultirateVo.setF_multirate_id(f_multirate_id);
				cmMultirateVo.setF_multirate_status(status);
				multirateDao.updateStatus(cmMultirateVo);
			}
			flag = 1;
		}
		return flag;
	}
}
